package com.zheng.api.server.controller;

import com.alibaba.fastjson.JSONObject;
import com.zheng.api.rpc.api.ApiService;
import com.zheng.common.base.BaseController;
import com.zheng.common.constant.MqConstant;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.activemq.ScheduledMessage;
import org.apache.activemq.command.ActiveMQQueue;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import springfox.documentation.annotations.ApiIgnore;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;
import javax.jms.TextMessage;

/**
 * 后台controller
 * Created by shuzheng on 2017/2/19.
 */

@Controller
@Api(value = "test", description = "test")
@RequestMapping(value = "/v1/test")
public class ApiTestController extends BaseController {

	private static final Logger LOGGER = LoggerFactory.getLogger(ApiTestController.class);

	@Autowired
	private ApiService apiService;
	@Autowired
	private JmsTemplate jmsQueueTemplate;

	@RequestMapping(value = "/helloWord")
	@ApiOperation(value = "helloWord")
	@ResponseBody
	public String helloWord(ModelMap modelMap) {

		return apiService.hello("world");
	}

	@ApiIgnore
	@RequestMapping(value = "/queueumbrellamachine")
	@ApiOperation(value = "queueumbrellamachine")
	@ResponseBody
	public String queueumbrellamachine(@RequestParam String command) {

		ActiveMQQueue queue =  new ActiveMQQueue(MqConstant.ZHENG_SERVER_QUEUE_UMBRELLA_MACHINE_NAME);
		JSONObject dataInfo = new JSONObject();
		final String msg = dataInfo.toJSONString();
		jmsQueueTemplate.send(queue, new MessageCreator() {
			public Message createMessage(Session session) throws JMSException {
				TextMessage tm = session.createTextMessage(msg);
				tm.setLongProperty(ScheduledMessage.AMQ_SCHEDULED_DELAY, 200);
				tm.setLongProperty(ScheduledMessage.AMQ_SCHEDULED_PERIOD, 1*1000);
				tm.setLongProperty(ScheduledMessage.AMQ_SCHEDULED_REPEAT, 1);
				tm.setLongProperty(ScheduledMessage.AMQ_SCHEDULED_REPEAT, 1);
				return tm;
			}
		});
		return apiService.hello("发送成功");
	}
	@ApiIgnore
	@RequestMapping(value = "/queueumbrellawechatapp")
	@ApiOperation(value = "queueumbrellawechatapp")
	@ResponseBody
	public String queueumbrellawechatapp(@RequestParam String command) {

		ActiveMQQueue queue =  new ActiveMQQueue(MqConstant.ZHENG_SERVER_QUEUE_UMBRELLA_WECHATAPP_NAME);
		JSONObject dataInfo = new JSONObject();
		//扫码借伞-雨伞识别结果上报
		if(command.equals("20101")){

		//扫码借伞-出伞结果上报
		}else if(command.equals("20102")){

		//扫码还伞结果上报
		}else if(command.equals("20105")){

		//物理还伞结果上报
		}else if(command.equals("20106")){

		}
		final String msg = dataInfo.toJSONString();
		jmsQueueTemplate.send(queue, new MessageCreator() {
            public Message createMessage(Session session) throws JMSException {
                TextMessage tm = session.createTextMessage(msg);
                tm.setLongProperty(ScheduledMessage.AMQ_SCHEDULED_DELAY, 200);
                tm.setLongProperty(ScheduledMessage.AMQ_SCHEDULED_PERIOD, 1*1000);
                tm.setLongProperty(ScheduledMessage.AMQ_SCHEDULED_REPEAT, 1);
                tm.setLongProperty(ScheduledMessage.AMQ_SCHEDULED_REPEAT, 1);
                return tm;
            }
        });

		return apiService.hello("发送成功");
	}

	@ApiIgnore
	@RequestMapping(value = "/topicumbrellamachine")
	@ApiOperation(value = "topicumbrellamachine")
	@ResponseBody
	public String topicumbrellamachine(@RequestParam String command) {

		ActiveMQQueue queue =  new ActiveMQQueue(MqConstant.ZHENG_SERVER_QUEUE_UMBRELLA_MACHINE_NAME);
		JSONObject dataInfo = new JSONObject();


		final String msg = dataInfo.toJSONString();
		jmsQueueTemplate.send(queue, new MessageCreator() {
			public Message createMessage(Session session) throws JMSException {
				TextMessage tm = session.createTextMessage(msg);
				tm.setLongProperty(ScheduledMessage.AMQ_SCHEDULED_DELAY, 200);
				tm.setLongProperty(ScheduledMessage.AMQ_SCHEDULED_PERIOD, 1*1000);
				tm.setLongProperty(ScheduledMessage.AMQ_SCHEDULED_REPEAT, 1);
				tm.setLongProperty(ScheduledMessage.AMQ_SCHEDULED_REPEAT, 1);
				return tm;
			}
		});
		return apiService.hello("发送成功");
	}
	@ApiIgnore
	@RequestMapping(value = "/topicumbrellawechatapp")
	@ApiOperation(value = "topicumbrellawechatapp")
	@ResponseBody
	public String topicumbrellawechatapp(@RequestParam String command) {

		ActiveMQQueue queue =  new ActiveMQQueue(MqConstant.ZHENG_SERVER_QUEUE_UMBRELLA_WECHATAPP_NAME);
		JSONObject dataInfo = new JSONObject();


		final String msg = dataInfo.toJSONString();
		jmsQueueTemplate.send(queue, new MessageCreator() {
			public Message createMessage(Session session) throws JMSException {
				TextMessage tm = session.createTextMessage(msg);
				tm.setLongProperty(ScheduledMessage.AMQ_SCHEDULED_DELAY, 200);
				tm.setLongProperty(ScheduledMessage.AMQ_SCHEDULED_PERIOD, 1*1000);
				tm.setLongProperty(ScheduledMessage.AMQ_SCHEDULED_REPEAT, 1);
				tm.setLongProperty(ScheduledMessage.AMQ_SCHEDULED_REPEAT, 1);
				return tm;
			}
		});

		return apiService.hello("发送成功");
	}
}